﻿using System;
using System.Collections.Generic;
using System.Text;
using BCDManager.Lib.ROOT.WMI;
using System.Management;

namespace BCDManager.Lib
{
	public static class BcdObjectExtensions
	{
		public static IDictionary<string, string> EnumProperties(this BcdObject obj)
		{
			IDictionary<string, string> values = new Dictionary<string, string>();

			ParseEnum(obj, typeof(Enumerations.BcdBootMgrElementTypes), ref values);
			ParseEnum(obj, typeof(Enumerations.BcdLibrary_SafeBoot), ref values);
			ParseEnum(obj, typeof(Enumerations.BcdLibraryElementTypes), ref values);
			ParseEnum(obj, typeof(Enumerations.BcdOSLoaderElementTypes), ref values);

			return values;
		}

		private static void ParseEnum(BcdObject obj, Type type, ref IDictionary<string, string> values)
		{
			ManagementBaseObject myObj;
			foreach (string item in Enum.GetNames(type))
			{
				string txt = contains(item);
				if (txt == string.Empty)
					continue;

				uint value = (uint)Enum.Parse(type, item);
				string pro = (obj.GetElement((uint)value, out myObj)) ?
					myObj.GetPropertyValue(txt).ToString() : string.Empty;
				if (!string.IsNullOrEmpty(pro))
					values.Add(item, pro);
			}
		}

		private static string contains(string pro)
		{
			if (pro.IndexOf("Integer") > -1) return "Integer";
			if (pro.IndexOf("Boolean") > -1) return "Boolean";
			if (pro.IndexOf("String") > -1) return "String";
			return string.Empty;
		}

	}
}
